# Replication code for Taylor C. Boas and Amy Erica Smith, “Looks Like Me, Thinks Like Me: Descriptive Representation and Opinion Congruence in Brazil.” Latin American Research Review 54, 2 (2019).

# Analysis conducted in R 3.6.0 on MacOS 10.13.6

# NOTE: This file replicates Appendix Figure 4. We recommend running R replication files in the following order; please see readme.txt for details.
# 	1_merge_lapop.R
# 	2_merge_latinobarometro.R
# 	3_recode_reshape.R
# 	4_difference_in_distributions.R
# 	5_regressions.R
# 	6_civil_society_meeting.R
# 	7_mass_descriptives.R
# 	8_elite_descriptives.R
# 	9_mean_differences.R
#	10_difference_in_distributions_ks.R
#	11_elite_sample_simulation.R
#	12_converts_vs_lifelong.R

# Set working directory as appropriate
# setwd('~/Dropbox/brazil_leg_surveys/replication/')

# Clean desktop and load packages. Please make sure all necessary packages are installed.

library(Hmisc)

rm(list=ls(all=T))

load('elite.data.RData')
load('mass.data.RData')

# =======================================================
# Appendix Figure 4: Opinion Congruence: Mean Differences
# in Elite and Mass Positions
# =======================================================

# Functions for calculating mean differences

meandiff<-function(x,y) mean(x,na.rm=T) - mean(y,na.rm=T)

wtd.meandiff<-function(x,y,weights.x,weights.y) wtd.mean(x,weights.x) - wtd.mean(y,weights.y)

mass.data$mass_survey<-gsub('^([a-z]*)\\-{0,1}[0-9].*','\\1', mass.data$mass_id)

# Weights for economic regime preference, ideology. For masses, weighting each paired BLS wave equally. For elites, using proportions of each demographic category in each actual legislature.

mass.weight.econ.all<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)])))
mass.weight.econ.evang<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_evang)])))
mass.weight.econ.female<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_female)])))
mass.weight.econ.black<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_black)])))
mass.weight.econ.loweduc<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_loweduc)])))
mass.weight.econ.pt<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_party=='PT')])))
mass.weight.econ.psdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_party=='PSDB')])))
mass.weight.econ.pmdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_party=='PMDB')])))
mass.weight.econ.sp<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_uf=='SP')])))
mass.weight.econ.mg<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_uf=='MG')])))
mass.weight.ideol.all<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)])))
mass.weight.ideol.evang<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_evang)])))
mass.weight.ideol.female<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_female)])))
mass.weight.ideol.black<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_black)])))
mass.weight.ideol.loweduc<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_loweduc)])))
mass.weight.ideol.pt<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PT')])))
mass.weight.ideol.psdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PSDB')])))
mass.weight.ideol.pmdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PMDB')])))
mass.weight.ideol.sp<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_uf=='SP')])))
mass.weight.ideol.mg<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_uf=='MG')])))

mass.data$weight.econ.all<-ifelse(is.na(mass.data$mass_econ),NA,mass.weight.econ.all[match(mass.data$mass_blsyear, mass.weight.econ.all[,1]),2])
mass.data$weight.econ.evang<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_evang,NA,mass.weight.econ.evang[match(mass.data$mass_blsyear, mass.weight.econ.evang[,1]),2])
mass.data$weight.econ.female<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_female,NA,mass.weight.econ.female[match(mass.data$mass_blsyear, mass.weight.econ.female[,1]),2])
mass.data$weight.econ.black<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_black,NA,mass.weight.econ.black[match(mass.data$mass_blsyear, mass.weight.econ.black[,1]),2])
mass.data$weight.econ.loweduc<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_loweduc,NA,mass.weight.econ.loweduc[match(mass.data$mass_blsyear, mass.weight.econ.loweduc[,1]),2])
mass.data$weight.econ.pt<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_party=='PT',NA,mass.weight.econ.pt[match(mass.data$mass_blsyear, mass.weight.econ.pt[,1]),2])
mass.data$weight.econ.psdb<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_party=='PSDB',NA,mass.weight.econ.psdb[match(mass.data$mass_blsyear, mass.weight.econ.psdb[,1]),2])
mass.data$weight.econ.pmdb<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_party=='PMDB',NA,mass.weight.econ.pmdb[match(mass.data$mass_blsyear, mass.weight.econ.pmdb[,1]),2])
mass.data$weight.econ.sp<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_uf=='SP',NA,mass.weight.econ.sp[match(mass.data$mass_blsyear, mass.weight.econ.sp[,1]),2])
mass.data$weight.econ.mg<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_uf=='MG',NA,mass.weight.econ.mg[match(mass.data$mass_blsyear, mass.weight.econ.mg[,1]),2])
mass.data$weight.ideol.all<-ifelse(is.na(mass.data$mass_ideol),NA,mass.weight.ideol.all[match(mass.data$mass_blsyear, mass.weight.ideol.all[,1]),2])
mass.data$weight.ideol.evang<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_evang,NA,mass.weight.ideol.evang[match(mass.data$mass_blsyear, mass.weight.ideol.evang[,1]),2])
mass.data$weight.ideol.female<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_female,NA,mass.weight.ideol.female[match(mass.data$mass_blsyear, mass.weight.ideol.female[,1]),2])
mass.data$weight.ideol.black<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_black,NA,mass.weight.ideol.black[match(mass.data$mass_blsyear, mass.weight.ideol.black[,1]),2])
mass.data$weight.ideol.loweduc<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_loweduc,NA,mass.weight.ideol.loweduc[match(mass.data$mass_blsyear, mass.weight.ideol.loweduc[,1]),2])
mass.data$weight.ideol.pt<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PT',NA,mass.weight.ideol.pt[match(mass.data$mass_blsyear, mass.weight.ideol.pt[,1]),2])
mass.data$weight.ideol.psdb<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PSDB',NA,mass.weight.ideol.psdb[match(mass.data$mass_blsyear, mass.weight.ideol.psdb[,1]),2])
mass.data$weight.ideol.pmdb<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PMDB',NA,mass.weight.ideol.pmdb[match(mass.data$mass_blsyear, mass.weight.ideol.pmdb[,1]),2])
mass.data$weight.ideol.sp<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_uf=='SP',NA,mass.weight.ideol.sp[match(mass.data$mass_blsyear, mass.weight.ideol.sp[,1]),2])
mass.data$weight.ideol.mg<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_uf=='MG',NA,mass.weight.ideol.mg[match(mass.data$mass_blsyear, mass.weight.ideol.mg[,1]),2])

elite.weight.econ.all<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)])))
elite.weight.econ.evang<-data.frame(c(.4,.2,.4)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_evang)])))
elite.weight.econ.female<-data.frame(c(.31,.345,.345)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_female)])))
elite.weight.econ.black<-data.frame((1/2)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_black)])))
elite.weight.econ.loweduc<-data.frame(c(.375,.3,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_loweduc)])))
elite.weight.econ.pt<-data.frame(c(.34,.32,.34)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_partythen=='PT')])))
elite.weight.econ.psdb<-data.frame(c(.37,.34,.29)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_partythen=='PSDB')])))
elite.weight.econ.pmdb<-data.frame(c(.325,.35,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_partythen=='PMDB')])))
elite.weight.econ.sp<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_uf=='SP')])))
elite.weight.econ.mg<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_uf=='MG')])))
elite.weight.ideol.all<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)])))
elite.weight.ideol.evang<-data.frame(c(.4,.2,.4)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_evang)])))
elite.weight.ideol.female<-data.frame(c(.31,.345,.345)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_female)])))
elite.weight.ideol.black<-data.frame((1/2)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_black)])))
elite.weight.ideol.loweduc<-data.frame(c(.375,.3,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_loweduc)])))
elite.weight.ideol.pt<-data.frame(c(.34,.32,.34)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PT')])))
elite.weight.ideol.psdb<-data.frame(c(.37,.34,.29)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PSDB')])))
elite.weight.ideol.pmdb<-data.frame(c(.325,.35,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PMDB')])))
elite.weight.ideol.sp<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_uf=='SP')])))
elite.weight.ideol.mg<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_uf=='MG')])))

elite.data$weight.econ.all<-ifelse(is.na(elite.data$elite_econ),NA,elite.weight.econ.all[match(elite.data$elite_year, elite.weight.econ.all[,1]),2])
elite.data$weight.econ.evang<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_evang,NA,elite.weight.econ.evang[match(elite.data$elite_year, elite.weight.econ.evang[,1]),2])
elite.data$weight.econ.female<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_female,NA,elite.weight.econ.female[match(elite.data$elite_year, elite.weight.econ.female[,1]),2])
elite.data$weight.econ.black<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_black,NA,elite.weight.econ.black[match(elite.data$elite_year, elite.weight.econ.black[,1]),2])
elite.data$weight.econ.loweduc<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_loweduc,NA,elite.weight.econ.loweduc[match(elite.data$elite_year, elite.weight.econ.loweduc[,1]),2])
elite.data$weight.econ.pt<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_partythen=='PT',NA,elite.weight.econ.pt[match(elite.data$elite_year, elite.weight.econ.pt[,1]),2])
elite.data$weight.econ.psdb<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_partythen=='PSDB',NA,elite.weight.econ.psdb[match(elite.data$elite_year, elite.weight.econ.psdb[,1]),2])
elite.data$weight.econ.pmdb<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_partythen=='PMDB',NA,elite.weight.econ.pmdb[match(elite.data$elite_year, elite.weight.econ.pmdb[,1]),2])
elite.data$weight.econ.sp<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_uf=='SP',NA,elite.weight.econ.sp[match(elite.data$elite_year, elite.weight.econ.sp[,1]),2])
elite.data$weight.econ.mg<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_uf=='MG',NA,elite.weight.econ.mg[match(elite.data$elite_year, elite.weight.econ.mg[,1]),2])
elite.data$weight.ideol.all<-ifelse(is.na(elite.data$elite_ideol),NA,elite.weight.ideol.all[match(elite.data$elite_year, elite.weight.ideol.all[,1]),2])
elite.data$weight.ideol.evang<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_evang,NA,elite.weight.ideol.evang[match(elite.data$elite_year, elite.weight.ideol.evang[,1]),2])
elite.data$weight.ideol.female<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_female,NA,elite.weight.ideol.female[match(elite.data$elite_year, elite.weight.ideol.female[,1]),2])
elite.data$weight.ideol.black<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_black,NA,elite.weight.ideol.black[match(elite.data$elite_year, elite.weight.ideol.black[,1]),2])
elite.data$weight.ideol.loweduc<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_loweduc,NA,elite.weight.ideol.loweduc[match(elite.data$elite_year, elite.weight.ideol.loweduc[,1]),2])
elite.data$weight.ideol.pt<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PT',NA,elite.weight.ideol.pt[match(elite.data$elite_year, elite.weight.ideol.pt[,1]),2])
elite.data$weight.ideol.psdb<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PSDB',NA,elite.weight.ideol.psdb[match(elite.data$elite_year, elite.weight.ideol.psdb[,1]),2])
elite.data$weight.ideol.pmdb<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PMDB',NA,elite.weight.ideol.pmdb[match(elite.data$elite_year, elite.weight.ideol.pmdb[,1]),2])
elite.data$weight.ideol.sp<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_uf=='SP',NA,elite.weight.ideol.sp[match(elite.data$elite_year, elite.weight.ideol.sp[,1]),2])
elite.data$weight.ideol.mg<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_uf=='MG',NA,elite.weight.ideol.mg[match(elite.data$elite_year, elite.weight.ideol.mg[,1]),2])

# Mean differences

meandiff.econ.all<-wtd.meandiff(elite.data$elite_econ,mass.data$mass_econ,elite.data$weight.econ.all,mass.data$mass_pweight*mass.data$weight.econ.all)
meandiff.econ.evang<-wtd.meandiff(elite.data$elite_econ[elite.data$elite_evang],mass.data$mass_econ[mass.data$mass_evang],elite.data$weight.econ.evang[elite.data$elite_evang],mass.data$mass_pweight[mass.data$mass_evang]*mass.data$weight.econ.evang[mass.data$mass_evang])
meandiff.econ.female<-wtd.meandiff(elite.data$elite_econ[elite.data$elite_female],mass.data$mass_econ[mass.data$mass_female],elite.data$weight.econ.female[elite.data$elite_female],mass.data$mass_pweight[mass.data$mass_female]*mass.data$weight.econ.female[mass.data$mass_female])
meandiff.econ.black<-wtd.meandiff(elite.data$elite_econ[elite.data$elite_black],mass.data$mass_econ[mass.data$mass_black],elite.data$weight.econ.black[elite.data$elite_black],mass.data$mass_pweight[mass.data$mass_black]*mass.data$weight.econ.black[mass.data$mass_black])
meandiff.econ.loweduc<-wtd.meandiff(elite.data$elite_econ[elite.data$elite_loweduc],mass.data$mass_econ[mass.data$mass_loweduc],elite.data$weight.econ.loweduc[elite.data$elite_loweduc],mass.data$mass_pweight[mass.data$mass_loweduc]*mass.data$weight.econ.loweduc[mass.data$mass_loweduc])
meandiff.econ.pt<-wtd.meandiff(elite.data$elite_econ[elite.data$elite_partythen=='PT'],mass.data$mass_econ[mass.data$mass_party=='PT'],elite.data$weight.econ.pt[elite.data$elite_partythen=='PT'],mass.data$mass_pweight[mass.data$mass_party=='PT']*mass.data$weight.econ.pt[mass.data$mass_party=='PT'])
meandiff.econ.psdb<-wtd.meandiff(elite.data$elite_econ[elite.data$elite_partythen=='PSDB'],mass.data$mass_econ[mass.data$mass_party=='PSDB'],elite.data$weight.econ.psdb[elite.data$elite_partythen=='PSDB'],mass.data$mass_pweight[mass.data$mass_party=='PSDB']*mass.data$weight.econ.psdb[mass.data$mass_party=='PSDB'])
meandiff.econ.pmdb<-wtd.meandiff(elite.data$elite_econ[elite.data$elite_partythen=='PMDB'],mass.data$mass_econ[mass.data$mass_party=='PMDB'],elite.data$weight.econ.pmdb[elite.data$elite_partythen=='PMDB'],mass.data$mass_pweight[mass.data$mass_party=='PMDB']*mass.data$weight.econ.pmdb[mass.data$mass_party=='PMDB'])
meandiff.econ.sp<-wtd.meandiff(elite.data$elite_econ[elite.data$elite_uf=='SP'],mass.data$mass_econ[mass.data$mass_uf=='SP'],elite.data$weight.econ.sp[elite.data$elite_uf=='SP'],mass.data$mass_pweight[mass.data$mass_uf=='SP']*mass.data$weight.econ.sp[mass.data$mass_uf=='SP'])
meandiff.econ.mg<-wtd.meandiff(elite.data$elite_econ[elite.data$elite_uf=='MG'],mass.data$mass_econ[mass.data$mass_uf=='MG'],elite.data$weight.econ.mg[elite.data$elite_uf=='MG'],mass.data$mass_pweight[mass.data$mass_uf=='MG']*mass.data$weight.econ.mg[mass.data$mass_uf=='MG'])

meandiff.ideol.all<-wtd.meandiff(elite.data$elite_ideol,mass.data$mass_ideol,elite.data$weight.ideol.all,mass.data$mass_pweight*mass.data$weight.ideol.all)
meandiff.ideol.evang<-wtd.meandiff(elite.data$elite_ideol[elite.data$elite_evang],mass.data$mass_ideol[mass.data$mass_evang],elite.data$weight.ideol.evang[elite.data$elite_evang],mass.data$mass_pweight[mass.data$mass_evang]*mass.data$weight.ideol.evang[mass.data$mass_evang])
meandiff.ideol.female<-wtd.meandiff(elite.data$elite_ideol[elite.data$elite_female],mass.data$mass_ideol[mass.data$mass_female],elite.data$weight.ideol.female[elite.data$elite_female],mass.data$mass_pweight[mass.data$mass_female]*mass.data$weight.ideol.female[mass.data$mass_female])
meandiff.ideol.black<-wtd.meandiff(elite.data$elite_ideol[elite.data$elite_black],mass.data$mass_ideol[mass.data$mass_black],elite.data$weight.ideol.black[elite.data$elite_black],mass.data$mass_pweight[mass.data$mass_black]*mass.data$weight.ideol.black[mass.data$mass_black])
meandiff.ideol.loweduc<-wtd.meandiff(elite.data$elite_ideol[elite.data$elite_loweduc],mass.data$mass_ideol[mass.data$mass_loweduc],elite.data$weight.ideol.loweduc[elite.data$elite_loweduc],mass.data$mass_pweight[mass.data$mass_loweduc]*mass.data$weight.ideol.loweduc[mass.data$mass_loweduc])
meandiff.ideol.pt<-wtd.meandiff(elite.data$elite_ideol[elite.data$elite_partythen=='PT'],mass.data$mass_ideol[mass.data$mass_party=='PT'],elite.data$weight.ideol.pt[elite.data$elite_partythen=='PT'],mass.data$mass_pweight[mass.data$mass_party=='PT']*mass.data$weight.ideol.pt[mass.data$mass_party=='PT'])
meandiff.ideol.psdb<-wtd.meandiff(elite.data$elite_ideol[elite.data$elite_partythen=='PSDB'],mass.data$mass_ideol[mass.data$mass_party=='PSDB'],elite.data$weight.ideol.psdb[elite.data$elite_partythen=='PSDB'],mass.data$mass_pweight[mass.data$mass_party=='PSDB']*mass.data$weight.ideol.psdb[mass.data$mass_party=='PSDB'])
meandiff.ideol.pmdb<-wtd.meandiff(elite.data$elite_ideol[elite.data$elite_partythen=='PMDB'],mass.data$mass_ideol[mass.data$mass_party=='PMDB'],elite.data$weight.ideol.pmdb[elite.data$elite_partythen=='PMDB'],mass.data$mass_pweight[mass.data$mass_party=='PMDB']*mass.data$weight.ideol.pmdb[mass.data$mass_party=='PMDB'])
meandiff.ideol.sp<-wtd.meandiff(elite.data$elite_ideol[elite.data$elite_uf=='SP'],mass.data$mass_ideol[mass.data$mass_uf=='SP'],elite.data$weight.ideol.sp[elite.data$elite_uf=='SP'],mass.data$mass_pweight[mass.data$mass_uf=='SP']*mass.data$weight.ideol.sp[mass.data$mass_uf=='SP'])
meandiff.ideol.mg<-wtd.meandiff(elite.data$elite_ideol[elite.data$elite_uf=='MG'],mass.data$mass_ideol[mass.data$mass_uf=='MG'],elite.data$weight.ideol.mg[elite.data$elite_uf=='MG'],mass.data$mass_pweight[mass.data$mass_uf=='MG']*mass.data$weight.ideol.mg[mass.data$mass_uf=='MG'])

meandiff.affirm.all<-wtd.meandiff(elite.data$elite_affirm,mass.data$mass_affirm, NULL,mass.data$mass_pweight)
meandiff.affirm.evang<-wtd.meandiff(elite.data$elite_affirm[elite.data$elite_evang],mass.data$mass_affirm[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
meandiff.affirm.female<-wtd.meandiff(elite.data$elite_affirm[elite.data$elite_female],mass.data$mass_affirm[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
meandiff.affirm.black<-wtd.meandiff(elite.data$elite_affirm[elite.data$elite_black],mass.data$mass_affirm[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
meandiff.affirm.loweduc<-wtd.meandiff(elite.data$elite_affirm[elite.data$elite_loweduc],mass.data$mass_affirm[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
meandiff.affirm.pt<-wtd.meandiff(elite.data$elite_affirm[elite.data$elite_partythen=='PT'],mass.data$mass_affirm[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
meandiff.affirm.psdb<-wtd.meandiff(elite.data$elite_affirm[elite.data$elite_partythen=='PSDB'],mass.data$mass_affirm[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
meandiff.affirm.pmdb<-wtd.meandiff(elite.data$elite_affirm[elite.data$elite_partythen=='PMDB'],mass.data$mass_affirm[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
meandiff.affirm.sp<-wtd.meandiff(elite.data$elite_affirm[elite.data$elite_uf=='SP'],mass.data$mass_affirm[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
meandiff.affirm.mg<-wtd.meandiff(elite.data$elite_affirm[elite.data$elite_uf=='MG'],mass.data$mass_affirm[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])


meandiff.classaffirm.all<-wtd.meandiff(elite.data$elite_classaffirm,mass.data$mass_classaffirm, NULL,mass.data$mass_pweight)
meandiff.classaffirm.evang<-wtd.meandiff(elite.data$elite_classaffirm[elite.data$elite_evang],mass.data$mass_classaffirm[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
meandiff.classaffirm.female<-wtd.meandiff(elite.data$elite_classaffirm[elite.data$elite_female],mass.data$mass_classaffirm[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
meandiff.classaffirm.black<-wtd.meandiff(elite.data$elite_classaffirm[elite.data$elite_black],mass.data$mass_classaffirm[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
meandiff.classaffirm.loweduc<-wtd.meandiff(elite.data$elite_classaffirm[elite.data$elite_loweduc],mass.data$mass_classaffirm[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
meandiff.classaffirm.pt<-wtd.meandiff(elite.data$elite_classaffirm[elite.data$elite_partythen=='PT'],mass.data$mass_classaffirm[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
meandiff.classaffirm.psdb<-wtd.meandiff(elite.data$elite_classaffirm[elite.data$elite_partythen=='PSDB'],mass.data$mass_classaffirm[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
meandiff.classaffirm.pmdb<-wtd.meandiff(elite.data$elite_classaffirm[elite.data$elite_partythen=='PMDB'],mass.data$mass_classaffirm[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
meandiff.classaffirm.sp<-wtd.meandiff(elite.data$elite_classaffirm[elite.data$elite_uf=='SP'],mass.data$mass_classaffirm[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
meandiff.classaffirm.mg<-wtd.meandiff(elite.data$elite_classaffirm[elite.data$elite_uf=='MG'],mass.data$mass_classaffirm[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])

meandiff.abortion.all<-wtd.meandiff(elite.data$elite_abortion,mass.data$mass_abortion, NULL,mass.data$mass_pweight)
meandiff.abortion.evang<-wtd.meandiff(elite.data$elite_abortion[elite.data$elite_evang],mass.data$mass_abortion[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
meandiff.abortion.female<-wtd.meandiff(elite.data$elite_abortion[elite.data$elite_female],mass.data$mass_abortion[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
meandiff.abortion.black<-wtd.meandiff(elite.data$elite_abortion[elite.data$elite_black],mass.data$mass_abortion[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
meandiff.abortion.loweduc<-wtd.meandiff(elite.data$elite_abortion[elite.data$elite_loweduc],mass.data$mass_abortion[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
meandiff.abortion.pt<-wtd.meandiff(elite.data$elite_abortion[elite.data$elite_partythen=='PT'],mass.data$mass_abortion[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
meandiff.abortion.psdb<-wtd.meandiff(elite.data$elite_abortion[elite.data$elite_partythen=='PSDB'],mass.data$mass_abortion[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
meandiff.abortion.pmdb<-wtd.meandiff(elite.data$elite_abortion[elite.data$elite_partythen=='PMDB'],mass.data$mass_abortion[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
meandiff.abortion.sp<-wtd.meandiff(elite.data$elite_abortion[elite.data$elite_uf=='SP'],mass.data$mass_abortion[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
meandiff.abortion.mg<-wtd.meandiff(elite.data$elite_abortion[elite.data$elite_uf=='MG'],mass.data$mass_abortion[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])

meandiff.gaymarriage.all<-wtd.meandiff(elite.data$elite_gaymarriage,mass.data$mass_gaymarriage, NULL,mass.data$mass_pweight)
meandiff.gaymarriage.evang<-wtd.meandiff(elite.data$elite_gaymarriage[elite.data$elite_evang],mass.data$mass_gaymarriage[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
meandiff.gaymarriage.female<-wtd.meandiff(elite.data$elite_gaymarriage[elite.data$elite_female],mass.data$mass_gaymarriage[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
meandiff.gaymarriage.black<-wtd.meandiff(elite.data$elite_gaymarriage[elite.data$elite_black],mass.data$mass_gaymarriage[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
meandiff.gaymarriage.loweduc<-wtd.meandiff(elite.data$elite_gaymarriage[elite.data$elite_loweduc],mass.data$mass_gaymarriage[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
meandiff.gaymarriage.pt<-wtd.meandiff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PT'],mass.data$mass_gaymarriage[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
meandiff.gaymarriage.psdb<-wtd.meandiff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PSDB'],mass.data$mass_gaymarriage[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
meandiff.gaymarriage.pmdb<-wtd.meandiff(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PMDB'],mass.data$mass_gaymarriage[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
meandiff.gaymarriage.sp<-wtd.meandiff(elite.data$elite_gaymarriage[elite.data$elite_uf=='SP'],mass.data$mass_gaymarriage[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
meandiff.gaymarriage.mg<-wtd.meandiff(elite.data$elite_gaymarriage[elite.data$elite_uf=='MG'],mass.data$mass_gaymarriage[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])

# Environment: unweighted because not drawing from BEPS or AB 2012.

meandiff.environment.all<-meandiff(elite.data$elite_environment,mass.data$mass_environment)
meandiff.environment.evang<-meandiff(elite.data$elite_environment[elite.data$elite_evang],mass.data$mass_environment[mass.data$mass_evang])
meandiff.environment.female<-meandiff(elite.data$elite_environment[elite.data$elite_female],mass.data$mass_environment[mass.data$mass_female])
meandiff.environment.black<-meandiff(elite.data$elite_environment[elite.data$elite_black],mass.data$mass_environment[mass.data$mass_black])
meandiff.environment.loweduc<-meandiff(elite.data$elite_environment[elite.data$elite_loweduc],mass.data$mass_environment[mass.data$mass_loweduc])
meandiff.environment.pt<-meandiff(elite.data$elite_environment[elite.data$elite_partythen=='PT'],mass.data$mass_environment[mass.data$mass_party=='PT'])
meandiff.environment.psdb<-meandiff(elite.data$elite_environment[elite.data$elite_partythen=='PSDB'],mass.data$mass_environment[mass.data$mass_party=='PSDB'])
meandiff.environment.pmdb<-meandiff(elite.data$elite_environment[elite.data$elite_partythen=='PMDB'],mass.data$mass_environment[mass.data$mass_party=='PMDB'])
meandiff.environment.sp<-meandiff(elite.data$elite_environment[elite.data$elite_uf=='SP'],mass.data$mass_environment[mass.data$mass_uf=='SP'])
meandiff.environment.mg<-meandiff(elite.data$elite_environment[elite.data$elite_uf=='MG'],mass.data$mass_environment[mass.data$mass_uf=='MG'])

meandiff.table <-matrix(c(
	meandiff.econ.all, meandiff.econ.evang, meandiff.econ.female, meandiff.econ.black, meandiff.econ.loweduc, meandiff.econ.pt, meandiff.econ.psdb, meandiff.econ.pmdb, meandiff.econ.sp, meandiff.econ.mg,
	meandiff.ideol.all, meandiff.ideol.evang, meandiff.ideol.female, meandiff.ideol.black, meandiff.ideol.loweduc, meandiff.ideol.pt, meandiff.ideol.psdb, meandiff.ideol.pmdb, meandiff.ideol.sp, meandiff.ideol.mg,
	meandiff.affirm.all, meandiff.affirm.evang, meandiff.affirm.female, meandiff.affirm.black, meandiff.affirm.loweduc, meandiff.affirm.pt, meandiff.affirm.psdb, meandiff.affirm.pmdb, meandiff.affirm.sp, meandiff.affirm.mg,
	meandiff.classaffirm.all, meandiff.classaffirm.evang, meandiff.classaffirm.female, meandiff.classaffirm.black, meandiff.classaffirm.loweduc, meandiff.classaffirm.pt, meandiff.classaffirm.psdb, meandiff.classaffirm.pmdb, meandiff.classaffirm.sp, meandiff.classaffirm.mg,
	meandiff.abortion.all, meandiff.abortion.evang, meandiff.abortion.female, meandiff.abortion.black, meandiff.abortion.loweduc, meandiff.abortion.pt, meandiff.abortion.psdb, meandiff.abortion.pmdb, meandiff.abortion.sp, meandiff.abortion.mg,
	meandiff.gaymarriage.all, meandiff.gaymarriage.evang, meandiff.gaymarriage.female, meandiff.gaymarriage.black, meandiff.gaymarriage.loweduc, meandiff.gaymarriage.pt, meandiff.gaymarriage.psdb, meandiff.gaymarriage.pmdb, meandiff.gaymarriage.sp, meandiff.gaymarriage.mg,
	meandiff.environment.all, meandiff.environment.evang, meandiff.environment.female, meandiff.environment.black, meandiff.environment.loweduc, meandiff.environment.pt, meandiff.environment.psdb, meandiff.environment.pmdb, meandiff.environment.sp, meandiff.environment.mg),ncol=10,byrow=T)
meandiff.table <-round(meandiff.table,3)
rownames(meandiff.table)<-c('Economic\nRegime\n(2002-13)','Ideology\n(2002-13)','Race\nQuotas\n(2010-13)','Class\nQuotas\n(2010-13)','Abortion\n(2010-13)','Gay\nMarriage\n(2010-13)','Environment\n(2011-13)')
colnames(meandiff.table)<-c('All','Evangelicals','Women','Afro-Brazilians','Low Education','PT','PSDB','PMDB','SP','MG')

# Plot
y<-matrix(7:1*2,ncol=10,nrow=7) + matrix(seq(-.9,.9,.2)*.75,ncol=10,nrow=7,byrow=T)

pdf(file='meandiff_points_plot.pdf',height=10,width=7.5)
par(mar=c(5,6,2,2)+.1)
plot(meandiff.table,y,pch=matrix(c(8,21:24,21:23,21:22),nrow=7,ncol=10,byrow=T),bg=matrix(c(NA,rep('black',4),rep('grey',3),rep('white',2)),nrow=7,ncol=10,byrow=T),xlab='Difference in Means (Elites - Masses)',yaxt='n',ylab='',xlim=c(-.6,.6),ylim=range(y))
abline(h=(9:2)*2-1,lty=3,lwd=1)
abline(v=0,lty=2,lwd=2)
legend('topright',legend=c('All','Evangelical','Female','Afro-Brazilian','No College','PT','PSDB','PMDB','SP','MG'),pch=c(8,21:24,21:23,21:22),pt.bg=c(NA,rep('black',4),rep('grey',3),rep('white',2)),bg='white')
Axis(side=2,at=(7:1)*2,labels=rownames(meandiff.table),las=2,hadj=0,tick=F,line=4)
dev.off()
